﻿namespace MDXStudio
{
    using MDXStudio.QueryEditor;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using WeifenLuo.WinFormsUI.Docking;

    public class QueryWindow : DockContent
    {
        private AutoCompleteList autoCompleteList;
        private IContainer components;
        private ImageList imageListIntellisense;
        internal Connection m_Connection;
        private bool m_fChanged;
        private Intellisense m_Intellisense;
        private MdxEditor mdxEditor;

        public QueryWindow()
        {
            this.InitializeComponent();
            this.m_fChanged = false;
        }

        private void autoCompleteList_Click(object sender, EventArgs e)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(QueryWindow));
            this.imageListIntellisense = new ImageList(this.components);
            this.autoCompleteList = new AutoCompleteList();
            this.mdxEditor = new MdxEditor(this.components);
            base.SuspendLayout();
            this.imageListIntellisense.ImageStream = (ImageListStreamer) manager.GetObject("imageListIntellisense.ImageStream");
            this.imageListIntellisense.TransparentColor = Color.Transparent;
            this.imageListIntellisense.Images.SetKeyName(0, "DbDimension.ico");
            this.imageListIntellisense.Images.SetKeyName(1, "Hierarchy.ico");
            this.imageListIntellisense.Images.SetKeyName(2, "Level2.ico");
            this.imageListIntellisense.Images.SetKeyName(3, "Measure.ico");
            this.imageListIntellisense.Images.SetKeyName(4, "MeasureCalculated.ico");
            this.imageListIntellisense.Images.SetKeyName(5, "Member.ico");
            this.imageListIntellisense.Images.SetKeyName(6, "Cube.ico");
            this.imageListIntellisense.Images.SetKeyName(7, "Function.ico");
            this.imageListIntellisense.Images.SetKeyName(8, "Property.ico");
            this.imageListIntellisense.Images.SetKeyName(9, "Keyword.ico");
            this.autoCompleteList.BorderStyle = BorderStyle.FixedSingle;
            this.autoCompleteList.DrawMode = DrawMode.OwnerDrawFixed;
            this.autoCompleteList.Font = new Font("Tahoma", 8f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.autoCompleteList.FormattingEnabled = true;
            this.autoCompleteList.ImageList = this.imageListIntellisense;
            this.autoCompleteList.ItemHeight = 0x12;
            this.autoCompleteList.Location = new Point(60, 0x2d);
            this.autoCompleteList.Name = "autoCompleteList";
            this.autoCompleteList.Size = new Size(0xad, 0xb6);
            this.autoCompleteList.Sorted = true;
            this.autoCompleteList.TabIndex = 6;
            this.autoCompleteList.UseTabStops = false;
            this.autoCompleteList.Visible = false;
            this.mdxEditor.AllowDrop = true;
            this.mdxEditor.Dock = DockStyle.Fill;
            this.mdxEditor.Font = new Font("Courier New", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.mdxEditor.Location = new Point(0, 0);
            this.mdxEditor.Name = "mdxEditor";
            this.mdxEditor.Size = new Size(0x2d3, 0x195);
            this.mdxEditor.TabIndex = 0;
            this.mdxEditor.Text = "";
            this.mdxEditor.WordWrap = false;
            this.mdxEditor.KeyDown += new KeyEventHandler(this.mdxEditor_KeyDown);
            this.mdxEditor.MouseDown += new MouseEventHandler(this.mdxEditor_MouseDown);
            this.mdxEditor.KeyPress += new KeyPressEventHandler(this.mdxEditor_KeyPress);
            this.mdxEditor.LostFocus += new EventHandler(this.mdxEditor_LostFocus);
            this.mdxEditor.TextChanged += new EventHandler(this.mdxEditor_TextChanged);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x2d3, 0x195);
            base.Controls.Add(this.autoCompleteList);
            base.Controls.Add(this.mdxEditor);
            base.DockAreas = DockAreas.Document;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.Name = "QueryWindow";
            base.TabText = "Query";
            this.Text = "Query";
            base.Load += new EventHandler(this.QueryWindow_Load);
            base.FormClosing += new FormClosingEventHandler(this.QueryWindow_FormClosing);
            base.ResumeLayout(false);
        }

        private void mdxEditor_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5)
            {
                (base.ParentForm as MainForm).toolStripButtonExecute_Click(null, null);
                e.Handled = true;
            }
            else if (e.Control && (e.KeyCode == Keys.E))
            {
                (base.ParentForm as MainForm).toolStripButtonExecute_Click(null, null);
                e.Handled = true;
            }
            else if (e.Control && (e.KeyCode == Keys.P))
            {
                (base.ParentForm as MainForm).toolStripButtonParse_Click(null, null);
                e.Handled = true;
            }
            else
            {
                this.m_Intellisense.ProcessKeyDownEvent(sender, e);
            }
            base.OnKeyDown(e);
        }

        private void mdxEditor_KeyPress(object sender, KeyPressEventArgs e)
        {
            this.m_Intellisense.ProcessKeyPressEvent(sender, e, this.m_Connection);
            base.OnKeyPress(e);
        }

        private void mdxEditor_LostFocus(object sender, EventArgs e)
        {
        }

        private void mdxEditor_MouseDown(object sender, MouseEventArgs e)
        {
            this.m_Intellisense.ProcessMouseDownEvent(sender, e);
        }

        private void mdxEditor_TextChanged(object sender, EventArgs e)
        {
            this.Changed = true;
        }

        private void QueryWindow_FormClosing(object sender, EventArgs e)
        {
            if (this.m_fChanged && (MessageBox.Show("Query changed - do you want to save it ?", "Save", MessageBoxButtons.YesNo) == DialogResult.Yes))
            {
                if (base.Tag != null)
                {
                    this.MDXEditor.SaveFile(base.Tag.ToString(), RichTextBoxStreamType.PlainText);
                }
                else
                {
                    (base.ParentForm as MainForm).saveAsToolStripMenuItem_Click(null, null);
                }
            }
        }

        private void QueryWindow_Load(object sender, EventArgs e)
        {
            this.m_Intellisense = new Intellisense();
            this.m_Intellisense.m_AutoCompleteList = this.autoCompleteList;
            this.autoCompleteList.KeyDown += new KeyEventHandler(this.m_Intellisense.listBoxAutoComplete_KeyDown);
            this.autoCompleteList.Click += new EventHandler(this.m_Intellisense.listBoxAutoComplete_DoubleClick);
            this.autoCompleteList.DoubleClick += new EventHandler(this.m_Intellisense.listBoxAutoComplete_DoubleClick);
            this.autoCompleteList.SelectedIndexChanged += new EventHandler(this.m_Intellisense.listBoxAutoComplete_SelectedIndexChanged);
        }

        internal bool Changed
        {
            get
            {
                return this.m_fChanged;
            }
            set
            {
                if (this.m_fChanged != value)
                {
                    this.m_fChanged = value;
                    if (this.m_fChanged)
                    {
                        base.TabText = string.Format("{0}*", this.Text);
                    }
                    else
                    {
                        base.TabText = this.Text;
                    }
                }
            }
        }

        internal MdxEditor MDXEditor
        {
            get
            {
                return this.mdxEditor;
            }
        }
    }
}

